package com.example.todaynews.util;

import android.content.Context;
import android.util.Log;

import com.example.todaynews.bean.NewsInfo;
import com.example.todaynews.dao.NewsDataUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wj on 2016/8/22.
 */
public class ObtainDataUtils {
    public static final String NEWSURL = "http://v.juhe.cn/toutiao/index?type=&key=114656e714ffdc89cda659375640cb07";
    private static final String TAG = "ObtainDataUtils";


    public static List<NewsInfo> getAllNewsFromNetwork(Context context) {
        List<NewsInfo> list = new ArrayList<NewsInfo>();

        try {
            URL url = new URL(NEWSURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(10 * 1000);
            int code = connection.getResponseCode();
            Log.e(TAG, "getAllNewsFromNetwork: code =" + code);
            if (code == 200) {
                InputStream in = connection.getInputStream();
                String result = InputSteramToString(in);
                JSONObject jsonObject = new JSONObject(result);
                JSONObject jsonResult = jsonObject.getJSONObject("result");
                JSONArray jsonArray = jsonResult.getJSONArray("data");
                for (int i = 0, len = jsonArray.length(); i < len; i++) {
                    JSONObject object = (JSONObject) jsonArray.opt(i);
                    NewsInfo info = new NewsInfo();
                    info.setTitle(object.getString("title"));
                    info.setThumbnail_pic_s(object.getString("thumbnail_pic_s"));
                    info.setThumbnail_pic_s02(object.getString("thumbnail_pic_s02"));
                    info.setThumbnail_pic_s03(object.getString("thumbnail_pic_s03"));
                    info.setDate(object.getString("date"));
                    info.setRealtype(object.getString("realtype"));
                    info.setUrl(object.getString("url"));
                    Log.e(TAG, "onResponse: info.toString() ==" + info.toString());
                    list.add(info);
                }
                new NewsDataUtils(context).delete();
                new NewsDataUtils(context).saveNews(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public static List<NewsInfo> getAllNewsFromDatabase(Context context) {
        return new NewsDataUtils(context).getAllNews();
    }

    public static String InputSteramToString(InputStream in) {
        String result = "";
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            result = new String(out.toString().getBytes(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }


        return result;
    }
}
